<!--
描述：题库排重处理组件
开发人：fwb
开发日期：2017年8月22日
-->
<template lang="pug">
    div.kalix-article(v-if="pageShow")
        div.frame-header
            div.frame-header--cnt
                el-form(ref="searchForm" v-bind:model="formModel" v-bind:inline="true" v-on:submit.native.prevent="")
                    el-form-item(label="题型" prop="questionType" v-bind:rules="rules.questionType" label-width="65px")
                        kalix-dict-select(v-model="formModel.questionType" appName="enrolment" dictType="题型" v-on:dictSelected="dictSelected")
                    el-form-item(v-if="formModel.questionType === '5'" label="题型子类" prop="subType" label-width="75px")
                        kalix-dict-select(v-model="formModel.subType" appName="enrolment" dictType="主观题类型")
                    el-form-item(v-if="formModel.questionType === '6'" label="题型子类" prop="subType" label-width="75px")
                        kalix-dict-select(v-model="formModel.subType" appName="enrolment" dictType="面试题类型")
                    el-form-item(v-if="formModel.questionType === '1' || formModel.questionType === '2' || formModel.questionType === '3'"
                    label="试题类别" prop="type" label-width="75px")
                        kalix-dict-select(v-model="formModel.type" appName="enrolment" dictType="类别")
                    el-form-item
                        el-button(type="success" v-on:click="onQueryClick")
                            i.iconfont.icon-query
                            | 查询
                        el-button(type="primary" v-on:click="onResetClick")
                            i.iconfont.icon-reset
                            | 重置
        div.frame-article
            div.frame-item
                keep-alive
                    kalix-table(bizKey="firstQuestions" title='题库排重信息列表' v-bind:targetURL="firstQuestionURL"
                    v-bind:toolbarBtnList="toolbarBtnList" v-bind:btnList="btnList1" v-bind:customTableTool="customTableTool"
                    v-on:onTableRowClick="onTableRowClick" v-bind:isFixedColumn="isFixedColumn"
                    v-bind:attachmentReadOnly="true" ref="kalixTable1")
                        template(slot="tableColumnSlot")
                            el-table-column(prop="subTypeName" label="题型子类" align="center" width="150px")
                            el-table-column(prop="typeName" label="试题类别" align="center" width="180px")
                            el-table-column(prop="questionId" label="ID" align="center" width="80px")
                            el-table-column(prop="stem" label="题干" align="center" width="350px")
                                template(slot-scope="scope")
                                    template(v-if="scope.row.questionType === '2'")
                                        el-popover(trigger="hover" placement="top" width="350")
                                            | {{scope.row.stem}}
                                            br
                                            | A {{scope.row.answerA}}   B {{scope.row.answerB}}
                                            br
                                            | C {{scope.row.answerC}}   D {{scope.row.answerD}}
                                            div(slot="reference")
                                                template(v-if="scope.row.stem && scope.row.stem.length > 20")
                                                    | {{scope.row.stem.substr(0, 20)}}...
                                                template(v-else)
                                                    | {{scope.row.stem}}
                                    template(v-else)
                                        el-popover(trigger="hover" placement="top" width="350")
                                            | {{scope.row.stem}}
                                            div(slot="reference")
                                                template(v-if="scope.row.stem && scope.row.stem.length > 20")
                                                    | {{scope.row.stem.substr(0, 20)}}...
                                                template(v-else)
                                                    | {{scope.row.stem}}
                            el-table-column(prop="createBy" label="创建者" align="center" width="120px")
                            el-table-column(prop="creationDate" label="创建时间" align="center" width="180px")
                                template(slot-scope="scope")
                                    i(class="el-icon-time")
                                    span {{scope.row.creationDate}}
                            el-table-column(prop="checkFlag" label="审核状态" align="center" width="120px")
                                template(slot-scope="scope")
                                    template(v-if="scope.row.checkFlag === '1'")
                                        el-tag(type="success") 已通过
                                    template(v-else-if="scope.row.checkFlag === '2'")
                                        el-popover(trigger="hover" placement="top" width="120")
                                            | 审核未通过原因
                                            br
                                            | {{scope.row.checkReason}}
                                            div(slot="reference")
                                                el-tag(type="danger") 未通过
                                    template(v-else)
                                        el-tag 未审核
                            el-table-column(prop="checker" label="审核员" align="center" width="120px")
                            el-table-column(prop="checkDate" label="审核时间" align="center" width="180px")
                                template(slot-scope="scope")
                                    i(class="el-icon-time")
                                    span {{scope.row.checkDate}}
            div.frame-article--split
            div.frame-item
                keep-alive
                    kalix-table(bizKey="secondQuestions" title='试题重复信息列表' v-bind:targetURL="secondQuestionURL"
                    v-bind:toolbarBtnList="toolbarBtnList" v-bind:btnList="btnList2" v-bind:customTableTool="customTableTool"
                    v-bind:isFixedColumn="isFixedColumn" v-bind:appendCondition="appendCondition"
                    v-bind:attachmentReadOnly="true" ref="kalixTable2")
                        template(slot="tableColumnSlot")
                            el-table-column(prop="subTypeName" label="题型子类" align="center" width="150px")
                            el-table-column(prop="typeName" label="试题类别" align="center" width="180px")
                            el-table-column(prop="questionId" label="ID" align="center" width="80px")
                            el-table-column(prop="stem" label="题干" align="center" width="350px")
                                template(slot-scope="scope")
                                    template(v-if="scope.row.questionType === '2'")
                                        el-popover(trigger="hover" placement="top" width="350")
                                            | {{scope.row.stem}}
                                            br
                                            | A {{scope.row.answerA}}   B {{scope.row.answerB}}
                                            br
                                            | C {{scope.row.answerC}}   D {{scope.row.answerD}}
                                            div(slot="reference")
                                                template(v-if="scope.row.stem && scope.row.stem.length > 20")
                                                    | {{scope.row.stem.substr(0, 20)}}...
                                                template(v-else)
                                                    | {{scope.row.stem}}
                                    template(v-else)
                                        el-popover(trigger="hover" placement="top" width="350")
                                            | {{scope.row.stem}}
                                            div(slot="reference")
                                                template(v-if="scope.row.stem && scope.row.stem.length > 20")
                                                    | {{scope.row.stem.substr(0, 20)}}...
                                                template(v-else)
                                                    | {{scope.row.stem}}
                            el-table-column(prop="createBy" label="创建者" align="center" width="120px")
                            el-table-column(prop="creationDate" label="创建时间" align="center" width="180px")
                                template(slot-scope="scope")
                                    i(class="el-icon-time")
                                    span {{scope.row.creationDate}}
                            el-table-column(prop="checkFlag" label="审核状态" align="center" width="120px")
                                template(slot-scope="scope")
                                    template(v-if="scope.row.checkFlag === '1'")
                                        el-tag(type="success") 已通过
                                    template(v-else-if="scope.row.checkFlag === '2'")
                                        el-popover(trigger="hover" placement="top" width="120")
                                            | 审核未通过原因
                                            br
                                            | {{scope.row.checkReason}}
                                            div(slot="reference")
                                                el-tag(type="danger") 未通过
                                    template(v-else)
                                        el-tag 未审核
                            el-table-column(prop="checker" label="审核员" align="center" width="120px")
                            el-table-column(prop="checkDate" label="审核时间" align="center" width="180px")
                                template(slot-scope="scope")
                                    i(class="el-icon-time")
                                    span {{scope.row.checkDate}}
                            el-table-column(prop="similarityDesc" label="相似度说明" align="center" width="200px")
    div(v-else) 题库功能已关闭，如需使用请联系管理员！
</template>

<script type="text/ecmascript-6">
    import FormModel from './model'
    import {AllRepeateURL, RepeateURL} from '../../config.toml'
    import {questionRepeatedBtnList, questionRepeatedBtnList2} from '../config.js'
    /*import {availableQuestion} from '../../common.js'*/

    export default {
        name: 'kalix-enrolment-questionsrepeated',
        data() {
            return {
                pageShow: false,
                formModel: Object.assign({}, FormModel),
                rules: {
                    questionType: [{required: true, message: '请选择题型', trigger: 'change'}]
                },
                labelWidth: '120px',
                toolbarBtnList: [
                    {id: 'add', isShow: false}
                ],
                firstQuestionURL: AllRepeateURL,
                btnList1: questionRepeatedBtnList,
                secondQuestionURL: AllRepeateURL + '/-1',
                btnList2: questionRepeatedBtnList2,
                isFixedColumn: true,
                appendCondition: {}
            }
        },
        components: {},
        created() {
            /*availableQuestion(this.axios, this.$KalixMessage, flag => {
             this.pageShow = flag
             })*/
            this.pageShow = true
        },
        destroyed() {
        },
        activated() {
        },
        deactivated() {
        },
        methods: {
            dictSelected(dict) {
                this.formModel.subType = ''
                this.formModel.type = ''
            },
            // 查询
            onQueryClick() {
                this.$refs.searchForm.validate((valid) => {
                    if (valid) {
                        this.clearTableData(0)
                        this.firstQuestionRow = null
                        this.getSecondQuestionTableData(null)
                        let queryCondition = {}
                        queryCondition['questionType'] = this.formModel.questionType
                        if (this.formModel.subType && this.formModel.subType.length) {
                            queryCondition['subType'] = this.formModel.subType
                        }
                        if (this.formModel.type && this.formModel.type.length) {
                            queryCondition['type'] = this.formModel.type
                        }
                        let jsonStr = JSON.stringify(queryCondition)
                        let searchObjAll = {jsonStr: jsonStr}
                        this.$KalixEventBus.$emit(this.$KalixEventConfig.ON_SEARCH_BUTTON_CLICK, searchObjAll)
                    } else {
                        this.$KalixMessage.error('请检查输入项！')
                        return
                    }
                })
            },
            // 重置搜索框
            onResetClick() {
                this.$refs.searchForm.resetFields()
                this.clearTableData(0)
                this.firstQuestionRow = null
                this.getSecondQuestionTableData(null)
            },
            customTableTool(row, btnId, that) {
                switch (btnId) {
                    case 'saveRepeat': {
                        this.onSaveRepeat(row, that)
                        break
                    }
                    case 'deleteRepeat': {
                        this.onDeleteRepeat(row, that)
                        break
                    }
                }
            },
            onSaveRepeat(row, table) {
                let url = RepeateURL.replace('[beans]', row.questionBeans) + '/' + row.questionId
                this.axios.request({
                    method: 'PUT',
                    url: url,
                    params: {
                        questionId: row.questionId
                    }
                }).then(response => {
                    if (response.data.success) {
                        this.$KalixMessage.success(response.data.msg)
                    } else {
                        this.$KalixMessage.error(response.data.msg)
                    }
                    this.clearTableData(2)
                    table.refresh()
                }).catch(() => {
                })
            },
            onDeleteRepeat(row, table) {
                let url = RepeateURL.replace('[beans]', row.questionBeans) + '/' + row.questionId
                this.axios.request({
                    method: 'DELETE',
                    url: url,
                    params: {
                        questionId: row.questionId
                    }
                }).then(response => {
                    if (response.data.success) {
                        this.$KalixMessage.success(response.data.msg)
                    } else {
                        this.$KalixMessage.error(response.data.msg)
                    }
                    this.clearTableData(2)
                    table.refresh()
                }).catch(() => {
                })
            },
            onTableRowClick(row, event, column) {
                if (this.firstQuestionRow) {
                    if (this.firstQuestionRow.questionId !== row.questionId) {
                        // 执行查询试题重复数据
                        this.clearTableData(2)
                        this.firstQuestionRow = row
                        this.getSecondQuestionTableData(this.firstQuestionRow.questionId)
                    }
                } else {
                    // 执行查询试题重复数据
                    this.clearTableData(2)
                    this.firstQuestionRow = row
                    this.getSecondQuestionTableData(this.firstQuestionRow.questionId)
                }
            },
            // 查询试题重复信息列表数据
            getSecondQuestionTableData(firstQuestionId) {
                if (firstQuestionId) {
                    this.appendCondition = {
                        'questionType': this.formModel.questionType,
                        'firstQuestionId': firstQuestionId
                    }
                    this.secondQuestionURL = AllRepeateURL + '/' + firstQuestionId
                } else {
                    this.appendCondition = {}
                    this.secondQuestionURL = AllRepeateURL + '/-1'
                }
            },
            // 清除列表数据0全部，1题库排重信息，2试题重复信息
            clearTableData(num) {
                switch (num) {
                    // 清除所有列表数据
                    case 0 :
                        this.$refs.kalixTable1.clearData()
                        this.$refs.kalixTable2.clearData()
                        break
                    case 1 :
                        // 清除题库排重信息列表数据
                        this.$refs.kalixTable1.clearData()
                        break
                    case 2 :
                        // 清除试题重复信息列表数据
                        this.$refs.kalixTable2.clearData()
                        break
                    default :
                        this.$refs.kalixTable1.clearData()
                        this.$refs.kalixTable2.clearData()
                        break
                }
            }
        }
    }
</script>

<style scoped lang="stylus">
    .frame-header
        background-color rgb(255, 255, 255)
        padding-top 7px
        margin-bottom 7px

    .frame-article
        flex 1
        display flex
        flex-direction column
        .frame-article--split
            height 7px
        .frame-item
            flex 1
</style>
